package com.ld.control.business.region.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ld.control.business.region.mapper.RegionMapper;
import com.ld.control.business.region.model.RegionModel;
import com.ld.control.business.region.service.RegionService;
import com.ld.control.business.region.vo.RegionReqVo;
import com.ld.control.business.region.vo.RegionRespVo;
import com.ld.control.utils.JWTUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 放置设备的区域定义 服务实现类
 * </p>
 *
 * @author wangfeng
 * @since 2024-04-11
 */
@Service
public class RegionServiceImpl extends ServiceImpl<RegionMapper, RegionModel> implements RegionService {

    @Autowired
    private RegionMapper regionMapper;

    @Autowired
    private JWTUtils jwtUtils;

    @Override
    public List<RegionRespVo> getList() {
        return regionMapper.selectAll();
    }

    @Override
    public void add(RegionReqVo reqVo) {
        Long currentUserAccountId = jwtUtils.getCurrentUserAccountId();
        RegionModel regionModel = new RegionModel();
        BeanUtils.copyProperties(reqVo, regionModel);
        regionModel.setCreateBy(currentUserAccountId);
        regionModel.setCreateTime(new Date());
        regionMapper.insert(regionModel);
    }

    @Override
    public void update(RegionReqVo reqVo) {
        Long currentUserAccountId = jwtUtils.getCurrentUserAccountId();
        RegionModel regionModel = new RegionModel();
        BeanUtils.copyProperties(reqVo, regionModel);
        regionModel.setUpdateBy(currentUserAccountId);
        regionModel.setUpdateTime(new Date());
        regionMapper.updateById(regionModel);
    }

    @Override
    public void delete(Long id) {
        regionMapper.deleteById(id);
    }

    @Override
    public Map<Long, String> getNameMap() {
        List<RegionRespVo> regionRespVoList = regionMapper.selectAll();
        return regionRespVoList.stream().collect(Collectors.toMap(RegionRespVo::getId, RegionRespVo::getName));
    }
}
